﻿using Rox.Runtimes.Hardware.GPU;
using System;
using System.Linq;
using System.Management;
using static Rox.Runtimes.Hardware.GPU.NVIDIA;
using static Rox.Runtimes.LocalizedString;
using static Rox.Runtimes.LogLibraries;
namespace DocTool
{
    public class Program
    {
        static void Main(string[] args)
        {
            WriteLog.Info("Starting GPU Detection...");
            WriteLog.Info($"正在读取信息...");
            if (IsNVIDIA())
            {
                WriteLog.Info("检测到NVIDIA显卡，正在读取详细信息...");
                try
                {
                    WriteLog.Info($"专用显存: {GetGPUMemory()} GB");
                    WriteLog.Info($"共享内存: {GetGPUSharedMemory()} GB");
                    WriteLog.Info($"显存类型: {GetGPUMemoryType()}");
                    WriteLog.Info($"显存总线宽度: {GetGPUMemoryBusWidth()} bit");

                    WriteLog.Info($"GPU核心使用率: {GetGPUUsage()}%");
                    WriteLog.Info($"显存使用率: {GetGPUMemoryUsage()}%");
                    WriteLog.Info($"视频引擎使用率: {GetGPUVideoEngineUsage()}%");
                    // 温度信息
                    WriteLog.Info($"温度传感器(1): {GetGPUTemperature()} ℃");
                    WriteLog.Info($"风扇传感器(1): {GetCooler1RPM()}");
                    WriteLog.Info($"风扇传感器(2): {GetCooler2RPM()}");
                    WriteLog.Info($"风扇传感器(3): {GetCooler3RPM()}");

                    WriteLog.Info($"GPU架构: {GetGPUArchitecture()}");
                }
                catch (Exception ex)
                {
                    if (ex.Message == "32bit process running in a 64bit environment can't access NVIDIA API.")
                        WriteLog.Error($"32位应用程序无法在64位运行");
                    WriteLog.Error(_Exception_With_xKind("x", ex));
                }

            }
            else
            {
                General.Information s = General.GetInformation();
                WriteLog.Info($"检测到非NVIDIA显卡，正在读取基本信息...");
                WriteLog.Info($"显卡名称: {s.FullName}");
                WriteLog.Info($"显存: {s.Memory} GB");
                WriteLog.Info($"HDR是否支持: {s.IsHDRSupported}");
                WriteLog.Info($"共享显存: {s.SharedMemory} GB");
            }
        }
        public static bool IsNVIDIA()
        {
            using (var searcher = new ManagementObjectSearcher("select * from Win32_VideoController"))
            {
                foreach (var obj in searcher.Get().Cast<ManagementObject>())
                {
                    if (obj["AdapterCompatibility"]?.ToString() == "NVIDIA")
                        return true;
                    else
                        return false;
                }
                return false;
            }
        }
    }
}
